package com.share.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * 说明：Struts2类型转换器
 * <br/>作者：杨浩泉
 * <br/>日期：2010-9-4
 */
public class ConverterUtilDate extends StrutsTypeConverter {

	private static String DATE_STYLE = "yyyy-MM-dd";   
	  
    @SuppressWarnings("unchecked")
	@Override  
    public Object convertFromString(Map context, String[] values, Class toClass) {   
        Date date = null;   
        String dateString = null;   
        if (values != null && values.length > 0) {   
            dateString = values[0];   
            if (dateString != null) {   
                SimpleDateFormat format = new SimpleDateFormat(DATE_STYLE);   
                try {   
                    date = format.parse(dateString);   
                } catch (ParseException e) {   
                    date = null;   
                }   
            }   
        }   
        return date;   
    }

	@SuppressWarnings("unchecked")
	@Override
	public String convertToString(Map map, Object o) {
		 // 格式化为date格式的字符串   
        Date date = (Date) o;   
        String dateTimeString = new SimpleDateFormat(DATE_STYLE).format(date) ;
        return dateTimeString ;
	}   
}
